package com.superbytecode.stencil.wrapper;

import cn.hutool.json.JSONUtil;
import com.superbytecode.stencil.entities.TemplateContext;
import com.superbytecode.stencil.entities.code.ClassProperty;
import com.superbytecode.stencil.entities.DatasourceContext;
import com.superbytecode.stencil.repository.domain.TemplateGroup;
import com.superbytecode.stencil.entities.table.SQLTable;
import com.superbytecode.stencil.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: code-stencil
 * @Package: com.superbytecode.stencil.structure
 * @ClassName: CodeTemplateSelector
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/14 11:37
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.c0m Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Slf4j
public class CodeTemplateSelector {
    final private DatasourceContext context;
    final private TemplateGroup group;

    public static CodeTemplateSelector builder(DatasourceContext context, TemplateGroup group) {
        return new CodeTemplateSelector(context, group);
    }

    public List<TemplateContext> build() {
        List<TemplateContext> properties = new ArrayList<>();
        List<SQLTable> sqlTables = SQLTableSelector.builder(context).obtainTableColumns();
        log.info("sqlTables :{}", JSONUtil.toJsonStr(sqlTables));
        for (SQLTable sqlTable : sqlTables) {
            properties.add(propertiesSupply(sqlTable));
        }
        return properties;
    }

    private TemplateContext propertiesSupply(SQLTable sqlTable) {
        TemplateContext codeProperty = new TemplateContext();
        codeProperty.setSerialVersionId(sqlTable.obtainSerialVersionUID());
        codeProperty.setPackageName(this.obtainPackage());
        String delPrefix = this.group.getDelPrefix();
        codeProperty.setJavaBeanName(sqlTable.obtainJavaBeanName(delPrefix));
        codeProperty.setJavaBeanNameLF(sqlTable.obtainJavaBeanNameLF(delPrefix));
        // 表和列信息
        codeProperty.setTableName(sqlTable.getTableName());
        codeProperty.setComments(sqlTable.getComments());
        codeProperty.setColumns(sqlTable.getColumns());
        codeProperty.setJavaPkColumn(sqlTable.obtainColumnPrimaryKey());
        // 包含时间格式判断
        codeProperty.setHasBigDecimal(sqlTable.bigDecimalExists());
        codeProperty.setHasDataTime(sqlTable.dataTimeExists());
        codeProperty.setHasList(sqlTable.listExists());
        //
        codeProperty.setFileName(group.getFileName());
        codeProperty.setFilePath(group.getFilePath());
        // 其他: 预留
        codeProperty.setClassExt(obtainClassProperty());
        return codeProperty;
    }

    private ClassProperty obtainClassProperty() {
        // ClassExt
        ClassProperty codeProperty = new ClassProperty();
        codeProperty.setAuthor(group.getAuthor());
        codeProperty.setEmail(group.getEmail());
        codeProperty.setVersion(group.getVersion());
        codeProperty.setCopyright(group.getCopyright());
        codeProperty.setNotes(group.getNotes());
        codeProperty.setDatetime(null);
        return codeProperty;
    }

    private String obtainPackage() {
        String packageName = group.getPackageName();
        return StringUtil.isEmpty(packageName) ? "com.superbytecode" : packageName;
    }

    private CodeTemplateSelector(DatasourceContext context, TemplateGroup templateGroup) {
        this.context = context;
        this.group = templateGroup;
    }
}